package com.gmss.crawler.service;

import com.gmss.crawler.utils.CommonUtils;
import com.gmss.crawler.utils.CrawlerCommonUtils;
import com.gmss.crawler.utils.RegexUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class AutoAminerCrawlerService {

    public static List<Map<String, Object>> adminerSearch(String url) throws InterruptedException {
        // chrome浏览器
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\chromedriver_win32_2\\chromedriver.exe");
        WebDriver webDriver = new ChromeDriver();
        // firefox浏览器
//        System.setProperty("webdriver.gecko.driver", "C:\\Users\\user\\Downloads\\firefox\\geckodriver.exe");
//        WebDriver webDriver = new FirefoxDriver();
        webDriver.manage().window().maximize();
        webDriver.manage().deleteAllCookies();
        // 与浏览器同步非常重要，必须等待浏览器加载完毕
        webDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        List<Map<String, Object>> paperList = new ArrayList<>();
        try {
            Thread.sleep(CommonUtils.randomRange(1000, 1000 * 2));
            webDriver.get(url);

            // 年份
            List<WebElement> yearsElementList = webDriver.findElements(By.xpath("/html/body/div[1]/section/main/main/article/section[2]/div[2]/section/div[2]/div[2]/div/div[2]/div/span"));
//            List<WebElement> yearsElementList = webDriver.findElements(By.xpath("/html/body/div[1]/section/main/main/article/section[2]/div[2]/section/div[2]/div[2]/div/div[2]/div/span[28]"));
            System.out.println();
            List<Integer> yearsList = new ArrayList<>();

            for (int i = yearsElementList.size() - 1; i >= 0; i--) {
                yearsList.add(Integer.parseInt(yearsElementList.get(i).findElement(By.xpath("span[2]")).getText()));
                System.out.println(yearsElementList.get(i).findElement(By.xpath("span[2]")).getText());
                Thread.sleep(CommonUtils.randomRange(1000, 1000 * 2));
                webDriver.findElement(By.xpath("/html/body/div[1]/section/main/main/article/section[2]/div[2]/section/div[2]/div[2]/div/div[2]/div/span[" + (i + 1) + "]")).click();
                List<Map<String, Object>> aMinerPaperList = getAminerPaper(webDriver);
                if (CollectionUtils.isNotEmpty(aMinerPaperList))
                    paperList.addAll(aMinerPaperList);
            }
        } catch (Exception e) {
            webDriver.quit();
        }
        return paperList;
    }

    private static List<Map<String, Object>> getAminerPaper(WebDriver webDriver) {
        List<WebElement> paperWebElementList = webDriver.findElements(By.xpath("/html/body/div/section/main/main/article/section[2]/div[2]/section/div[2]/div[3]/div"));
        List<Map<String, Object>> paperList = new ArrayList<>();
        List<String> authorList = null;
        for (int i = 1; i <= paperWebElementList.size(); i++) {
            authorList = new ArrayList<>();
            // 所有作者
//                List<WebElement> authorsWebElementList = webDriver.findElements(By.xpath("/html/body/div[1]/section/main/main/article/section[2]/div[2]/section/div[2]/div[3]/div["+i+"]/div/div[2]/div/div/span"));
//                                List<String> authorList = new ArrayList<>();
//                if (CollectionUtils.isNotEmpty(authorsWebElementList)) {
//                    for (int j = 0; j <authorsWebElementList.size(); j++) {
//                        String author = authorsWebElementList.get(j).findElement(By.tagName("span")).findElement(By.tagName("span")).getText();
//                        authorList.add(author);
//                        System.out.println(authorsWebElementList.get(j).findElement(By.tagName("span")).findElement(By.tagName("span")).getText());
//                    }
//                }
            String href = webDriver.findElement(By.xpath("/html/body/div[1]/section/main/main/article/section[2]/div[2]/section/div[2]/div[3]/div[" + i + "]/div/div[1]/div/a")).getAttribute("href");
            webDriver.get(href);

            String title = webDriver.findElement(By.xpath("/html/body/div/section/main/main/article/section[2]/div[1]/h1/span")).getText();
            // 0：中文，1：外文'
            Integer languageStandard = null;
            if (CrawlerCommonUtils.hasChineseCharacter(title)) {
                languageStandard = 0;
            } else {
                languageStandard = 1;
            }
            // /html/body/div/section/main/main/article/section[3]/div[1]/div[2]
            String authorsXPath = "/html/body/div/section/main/main/article/section[3]/div[1]/div[2]/div";
            List<WebElement> authorsWebElementList = webDriver.findElements(By.xpath(authorsXPath));

            if (CollectionUtils.isNotEmpty(authorsWebElementList)) {
                for (int j = 0; j < authorsWebElementList.size(); j++) {
                    // /html/body/div/section/main/main/article/section[3]/div[1]/span/a
                    String author = authorsWebElementList.get(j).findElement(By.tagName("img")).getAttribute("alt");
                    System.out.println(author);
                    authorList.add(author);
                }
            }
            // 作者更多
            try {
                webDriver.findElement(By.xpath("/html/body/div/section/main/main/article/section[3]/div[1]/div[3]/a/span")).click();
                List<WebElement> otherAuthorWebDriverList = webDriver.findElements(By.xpath("/html/body/div/section/main/main/article/section[3]/div[1]/span"));
                if (CollectionUtils.isNotEmpty(otherAuthorWebDriverList)) {
                    for (WebElement webElement : otherAuthorWebDriverList) {
                        String otherAuthor = webElement.findElement(By.tagName("a")).getText();
                        if (StringUtils.isNotBlank(otherAuthor))
                            authorList.add(otherAuthor);
                    }
                }
            } catch (Exception e) {
            }

            String publishDate = "";
            String publisher = "";
            try {
                String publisherAndDate = webDriver.findElement(By.xpath("/html/body/div/section/main/main/article/section[2]/p")).getText();
                publisherAndDate = publisherAndDate.replaceAll("，", ",").replaceAll("（", "(").replaceAll("）", ")");
                publishDate = RegexUtils.getTextContent(publisherAndDate, RegexUtils.ALL_KUA_REGEX_TWO);
                if (publisherAndDate.indexOf(",") > 0) {
                    publisher = publisherAndDate.split(",")[0];
                }
            } catch (Exception e) {

            }

            // 更多
            try {
                webDriver.findElement(By.xpath("/html/body/div/section/main/main/article/section[2]/div[5]/div/span/span")).click();
            } catch (Exception e) {
            }
            String descData = null;
            try {
                descData = webDriver.findElement(By.xpath("/html/body/div/section/main/main/article/section[2]/div[5]/div")).getText();
                descData = descData.replaceAll("收起", "");
            } catch (Exception e) {
            }

            // 关键词
            List<WebElement> keywordWebElementList = webDriver.findElements(By.xpath("/html/body/div/section/main/main/article/section[2]/div[4]/div[2]/span"));
            List<String> keywordList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(keywordWebElementList)) {
                for (WebElement webElement : keywordWebElementList) {
//                        String keyword = webElement.findElement(By.cssSelector("a-aminer-core-pub-c-paper-keywords-keyWord")).findElement(By.tagName("a")).getText();
                    String keyword = webElement.findElement(By.tagName("a")).getText();
                    keywordList.add(keyword);
                }
            }
            System.out.println(keywordList);
            Map<String, Object> map = new HashMap<>();
            if (CollectionUtils.isNotEmpty(keywordList))
                map.put("keyword", StringUtils.join(keywordList, ","));
            if (CollectionUtils.isNotEmpty(authorList)) {
                map.put("authors", StringUtils.join(authorList, ","));
                map.put("firstAuthor", authorList.get(0));
            }
            if (StringUtils.isNotBlank(descData))
                map.put("descData", descData);
            if (StringUtils.isNotBlank(publisher))
                map.put("publisher", publisher);
            if (StringUtils.isNotBlank(publishDate))
                map.put("publishDate", publishDate);
            if (StringUtils.isNotBlank(title))
                map.put("title", title);
            if (null != languageStandard)
                map.put("languageStandard", languageStandard);

            if (MapUtils.isNotEmpty(map)) {
                paperList.add(map);
            }

            // 后退
            webDriver.navigate().back();
        }
        return paperList;
    }

    public static void main(String[] args) throws InterruptedException {
        adminerSearch("https://www.aminer.cn/profile/53f64553dabfaeaf590f3cce");
    }
}
